<template>
	<uni-nav-bar fixed="true" statusBar="true" border="false" left-icon="left" title="提现" @clickLeft="back"></uni-nav-bar>
	<view>
		<view class="p3080" >
			<view class="mt100"><text class="color-light-blue font_size_32">请输入提现金额</text></view>
			<view class="uni-flex sp_betw lh100 bor_b mb15">
				<view class="mb30">
					<input class="uni-input lh100 font-28 text-right" type="number" clearButton="auto" placeholder="" v-model="withdrawalValue" @input="validateWithdrawalValue"  />
				</view>
				<view class="num">￥{{withdrawalValue}}</view>
			</view>
			<view v-if="showWarning" class="color-red mb30">⚠️ 提现金额不能大于￥{{accountInfo.canUseBalance}}</view>
			<view class="color-gray mb30">可提现金额&nbsp;&nbsp;&nbsp;￥{{accountInfo.canUseBalance}}</view>
			<view class="mb30 lineInfo">税费提示：应税务部门监管要求，用户提现需上报个人经营所得税，平台将依据税费代缴后结算的金额提现，代扣税率随地区产生差异，平台会依据各地区最低适用税率进行申报。税费详情可在次月自行登录个税APP查询。</view>
			<view class="color-black mb30">选择提现账户</view>
			<view class="">
				<uni-data-checkbox v-model="withdrawalAccount" :localdata="withdrawalAccounts" />
			</view>
			<view class="tspos tc" v-if="withdrawalAccounts.length == 0">
				<view class="wd100">您尚未添加提现卡</view>
				<text class="color-blue" @tap="navgetTo('pages/my/create_withdrawal_card','','')">新增提现卡>></text>
			</view>
			<view class="tspos tc" v-if="withdrawalAccounts.length > 0">
				<text class="color-blue" @tap="navgetTo('pages/my/withdrawal_card','','')">管理提现卡>></text>
			</view>
		</view>
		<view class="fixed-bottom-wrap" @tap="withdraw">
			<view class="uni-flex jc bg-green">
				<view class="flex-item">
					<text class="color_white font_size_30 tc">确认提现</text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import api from '@/common/api/webapi.js'; /*api*/
	export default {
		data() {
			return {
				accountInfo:{},
				withdrawalValue:'',
				withdrawalAccount:0,
				navIndex: 0,
				// 单选数据源
				withdrawalAccounts: [],
				// 提现金额校验警告
				showWarning: false
			}   
		},
		onLoad(option) {
			this.GetDetailInfo();
		},
		methods: {
			back() {
				uni.navigateTo({
					url:'/pages/my/myWallet',
					success() {
						var page = getCurrentPages().pop();
						if (page == undefined || page == null) return;
						page.onLoad();
					}
				})
			},
			// 实时校验提现金额不大于可提现金额
			validateWithdrawalValue() {
				// 转换为数字进行比较
				const withdrawalValue = parseFloat(this.withdrawalValue || 0);
				const canUseBalance = parseFloat(this.accountInfo.canUseBalance || 0);
				
				// 如果提现金额大于可提现金额
				if (withdrawalValue > canUseBalance) {
					this.showWarning = true;
					// 限制提现金额为可提现金额
					this.withdrawalValue = canUseBalance.toString();
				} else {
					this.showWarning = false;
				}
			},
			bindDateChange: function(e) {
				this.date = e.detail.value;
				this.getProfitData();
			},
			checkIndex(index) {
				this.navIndex = index;
			},
			// 页面跳转
			navgetTo(page, param, type) {
				// let userLoginFlag = storageUtil.getStorageData("isLogin");
			
				// if (!userLoginFlag || userLoginFlag == '') {
				// 	this.popShowHandle();
				// 	return;
				// }
				if (param != undefined) {
					type = type || '';
					let data = {
						data: param,
						page: type
					}
					uni.navigateTo({
						url: "/" + page + "?param=" + encodeURI(JSON.stringify(data))
					});
				} else {
					uni.navigateTo({
						url: "/" + page
					});
				}
			},
			GetDetailInfo() {
				let _this = this;
				api.walletDetail().then(res => {
					if (res.code === 0) {
						_this.accountInfo = res.data;
						this.getBankList();
					} else {}
				}).catch()
			},
			getBankList(){
				let _this = this;
				api.bankList().then(res => {
				if (res.code === 0) {
						if (res.data.length > 0) {
							_this.withdrawalAccounts = res.data;
							_this.withdrawalAccounts.forEach((item,index) =>{
								let showBankNum = item.bankCardNum.slice(-4);
								item.text = `${item.bankName}(${showBankNum})`;
								item.value = `${item.bankCardNum}_${item.id}`;
							});
						}else{
							_this.withdrawalAccounts = [];
						}
					} else {}
				}).catch()
			},
			withdraw() {
				// 清除之前的警告
				this.showWarning = false;
				
				// 再次进行校验，确保提现金额不大于可提现金额
				const withdrawalValue = parseFloat(this.withdrawalValue || 0);
				const canUseBalance = parseFloat(this.accountInfo.canUseBalance || 0);
				
				// 如果提现金额为空，给出提示
				if (!this.withdrawalValue || this.withdrawalValue.trim() === '') {
					uni.showToast({
						icon: 'none',
						title: '提现金额不能为空',
						duration: 2000
					});
					return;
				}
				
				// 如果提现金额为0或负数，给出提示
				if (withdrawalValue <= 0) {
					uni.showToast({
						icon: 'none',
						title: '提现金额不能为0或负数',
						duration: 2000
					});
					return;
				}
				
				// 如果提现金额大于可提现金额，给出提示
				if (withdrawalValue > canUseBalance) {
					uni.showToast({
						icon: 'none',
						title: '提现金额不能大于可提现金额',
						duration: 2000
					});
					this.showWarning = true;
					this.withdrawalValue = canUseBalance.toString();
					return;
				}
				
				// 检查是否选择了提现账户
				if (!this.withdrawalAccount) {
					uni.showToast({
						icon: 'none',
						title: '请选择提现账户',
						duration: 2000
					});
					return;
				}
				
				// 执行提现操作
				let withdrawAccount = this.withdrawalAccount;
				let bankId = withdrawAccount.split("_")[1];
				let bankNum = withdrawAccount.split("_")[0];
				
				let requestData = {
					withdrawAccount:bankNum,
					withdrawAccountType:'BANK',
					withdrawAmount:this.withdrawalValue,
					bankId:bankId,
				};
				
				uni.showLoading({
					title: '处理中...',
					mask: true
				});
				
				api.withdraw(requestData).then(res => {
					uni.hideLoading();
					if (res.code === 0) {
						uni.navigateTo({
							url: '/pages/sucess/withdraw_sucess'
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg || '提现失败，请稍后重试',
							duration: 2000
						});
					}
				}).catch(error => {
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: '网络异常，请稍后重试',
						duration: 2000
					});
				});
			},
			// getProfitData(){
			// 	let _this = this;
			// 	let year = this.date.split("-")[0];
			// 	let month = this.date.split("-")[1];
			// 	let params = {
			// 		params:{
			// 			phone:uni.getStorageSync("phone"),
			// 			year:year,
			// 			month:month
			// 		}
			// 	}
			// 	uni.showLoading({
			// 		title: '加载中。。。',
			// 		mask:true,
			// 	})
			// 	api.getProfitData(params).then(res => {
			// 		if (res.code === 0) {
			// 			uni.hideLoading();
			// 			_this.incomeInfo = res.data;
			// 		} else {
			// 			uni.hideLoading();
			// 			uni.showToast({
			// 				icon: 'none',
			// 				title: res.msg
			// 			});
			// 		}
			// 	}).catch(e=>{
			// 		uni.hideLoading();
			// 	})
			// }
		}
	}
</script>

<style lang="scss">
	.withdrawable{
		width: 80%;
		margin: 40rpx auto;
	}
	.tspos{
		position: absolute;
		left: 0;
		right: 0;
		bottom: 300px;
	}
	::v-deep.uni-data-checklist .checklist-group{
		justify-content: flex-start;
	}
	::v-deep.uni-data-checklist .checklist-group .checklist-box{
		width: 100%;
	}

</style>
